require 'file/tail'

module Installr::Server::Command
  class Self < Base
    def log(remote_log_file, remote_log_length)
      puts "[self:log] ***** log #{remote_log_file} (#{remote_log_length})" if @options[:verbose]
      @log.info("[self:log] ***** log #{remote_log_file} (#{remote_log_length})")

      log_file = @options[:log_base_dir] + '/' + File.basename(remote_log_file)
      puts "log file = #{log_file}" if @options[:verbose]

      lines = ''
      if File.exist?(log_file) and !File.directory?(log_file)
        File::Tail::Logfile.open(log_file, :backward => remote_log_length.to_i, :return_if_eof => true ) do | log|
          log.tail { |line| lines += line }
        end
      else
        lines = "cannot find log file " + log_file
      end
      return lines
    end
  end
end
